package com.fa4j.system.account.repository.impl;

import com.fa4j.common.data.repository.impl.BaseRepositoryImpl;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.account.model.AccountBind;
import com.fa4j.system.account.repository.AccountBindRepository;
import com.fa4j.system.account.repository.mapper.AccountBindMapper;
import com.fa4j.system.login.model.LoginType;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 账号绑定 持久化接口 实现
 *
 * @since 2025-05-06 16:16:26
 */
@Repository
public class AccountBindRepositoryImpl extends BaseRepositoryImpl<AccountBindMapper, AccountBind> implements AccountBindRepository {

    @Override
    public Optional<AccountBind> findAccountByOpenId(AppType appType, LoginType loginType, String thirdAppId, String thirdOpenId) {
        return super.lambdaQuery()
                .eq(AccountBind::getAppType, appType)
                .eq(AccountBind::getType, loginType)
                .eq(AccountBind::getThirdAppId, thirdAppId)
                .eq(AccountBind::getThirdOpenId, thirdOpenId)
                .oneOpt();
    }

    @Override
    public List<AccountBind> queryAccountByUnionId(AppType appType, LoginType loginType, String thirdUnionId) {
        return super.lambdaQuery()
                .eq(AccountBind::getAppType, appType)
                .eq(AccountBind::getType, loginType)
                .eq(AccountBind::getThirdUnionId, thirdUnionId)
                .list();
    }

    @Override
    public List<AccountBind> queryByAccountIdAndType(Long accountId, LoginType loginType) {
        return super.lambdaQuery()
                .select(p -> !p.getColumn().equals("ext"))
                .eq(AccountBind::getAccountId, accountId)
                .eq(loginType != null, AccountBind::getType, loginType)
                .list();
    }
}
